<template>
  <article class="page">
    <headerbar>评价</headerbar>
    <section class="frame-content-sub no-bg">
      <ul class="top-menu">
        <li @click="clickNav('organization')" :class="{'active':routeName == 'organization'}"><span>培训机构</span></li>
        <li @click="clickNav('organizer')" :class="{'active':routeName == 'organizer'}"><span>主办单位</span></li>
        <li @click="clickNav('teacher')" :class="{'active':routeName == 'teacher'}"><span>教师</span></li>
      </ul>

      <!--<appraiseInput :prop="standard[routeName]" @onSubmit="save"></appraiseInput>-->
      <section class="block pdv-3 pdh-3" v-if="routeName == 'organization'">

        <div class="score-item" v-for="item in standard[routeName]">
          <div class="text-wrap">
            <div class="title">{{item.pfxm}} ({{item.score}})</div>
            <div class="tip">{{item.pfbz}}</div>
          </div>
          <div class="input-wrap" v-if="false">
            <input v-model="item.value" type="text">
          </div>
        </div>

        <div class="input-column-wrap">
        <div class="input-wrap">
          <input v-model="organizationApppraiseValue" maxlength="3" placeholder="请输入评分" type="text">
        </div>
        </div>

      </section>
      <section class="block pdv-3 pdh-3" v-if="routeName == 'organizer'">

        <div class="score-item" v-for="item in standard[routeName]">
          <div class="text-wrap">
            <div class="title">{{item.pfxm}} ({{item.score}})</div>
            <div class="tip">{{item.pfbz}}</div>
          </div>
          <div class="input-column-wrap">
          <div class="input-wrap" v-if="false">
            <input v-model="item.value" type="text">
          </div>
          </div>
        </div>

        <div class="input-column-wrap">
        <div class="input-wrap">
          <input v-model="organizerApppraiseValue" maxlength="3" placeholder="请输入评分" type="text">
        </div>
        </div>

      </section>
      <section class="block pdv-3 pdh-3" v-if="routeName == 'teacher'">

        <div class="score-item" v-for="item in standard[routeName]">
          <div class="text-wrap">
            <div class="title">{{item.pfxm}} ({{item.score}})</div>
            <div class="tip">{{item.pfbz}}</div>
          </div>
          <div class="input-column-wrap">
          <div class="input-wrap" v-if="false">
            <input v-model="item.value" type="text">
          </div>
          </div>
        </div>

        <!--<div class="input-wrap">
          <input v-model="teacherApppraiseValue" placeholder="请输入评分" type="text">
        </div>-->

        <div class="title">课程表：</div>

        <div class="score-item" v-for="item in list">
          <div class="text-wrap">
            <div class="title">课程：</div>
            <div class="tip">{{item.name}}</div>
          </div>
          <div class="text-wrap">
            <div class="title">教师：</div>
            <div class="tip">{{item.lecturerName}}</div>
          </div>
          <div class="input-column-wrap" style="width:15em;">
          <div class="input-wrap">
            <input v-model="item.value" maxlength="3" placeholder="请输入评分" type="text">
          </div>
          </div>
        </div>

      </section>

      <div class="pdv-3"></div>
      <div @click.stop="saveData()" class="default-block-btn">提交评价</div>
      <div class="pdv-2"></div>


      <section v-if="routeName==='teacher' && false" class="block pdv-3 pdh-3">

        <div class="score-item" v-for="item in list" @click="appraiseTeacher(item)">
          <div class="text-wrap">
            <div class="title"><span class="no-wrap">{{item.lecturerName}}</span> <i class="pdh-2"></i>  <div class=" tip">{{item.name}}</div></div>

          </div>
          <div class="btn">评价</div>
        </div>

        <!--<div class="pdv-3"></div>-->
        <!--<div @click="save()" class="default-block-btn">提交评价</div>-->
        <!--<div class="pdv-2"></div>-->

      </section>

    </section>
  </article>
</template>

<script>
  import tools from '@/utils/tools'
  import appraiseInput from '@/components/dangjian/home/appraiseInput'
  import _ from 'lodash'
  export default {
    name: 'appraiseManage',
    components: {appraiseInput},
    data () {
      return {
        routeName: 'organization',
        pageIndex: 1,
        pageSize: 10,
        list: [],
        options: {
          pullUpLoad: true,
          scrollbar: false
        },
        id: '',
        userId: '',
        detail: {},
        standard: {
          organization: [],
          organizer: [],
          teacher: []
        },
        appraiseDialogInstance: null,
        organizationApppraiseValue: '',
        organizerApppraiseValue: '',
        teacherApppraiseValue: ''
      }
    },
    created () {
      this.detail = this.$store.state.appraiseDetail
      this.userId = this.$store.state.userInfo.id
      this.id = this.$route.query.id
      this.clickNav('organization')
      for (let key in this.standard) {
        this.organization(key)
      }
    },
    methods: {
      clickNav (routeName) {
        let query = _.extend(this.$route.query, {routeName: routeName})
        if (this.$route.query.routeName !== routeName) {
          this.$router.replace({name: this.$route.name, query: query})
        }
        this.routeName = routeName
        this.pageIndex = 1
        this.list = []
        this.organization(routeName)
        this.classList()
      },
      appraiseTeacher (item) {
//        if (!this.appraiseDialogInstance) {
          this.$createAppraiseInputDialog({
            $props: {
              prop: this.standard.teacher
            },
            $events: {
              onSubmit: (data, apppraiseValue) => {
                this.saveTeacherAppraise(item, data, apppraiseValue)
              }
            }
          }).show()
//        }
//        this.appraiseDialogInstance.show()
      },
      saveTeacherAppraise (item, data, apppraiseValue) {
        console.log(item.lecturerId)
        console.log(item.lecturerName)
        let baseParams = {
          classId: '',
          curriculumName: '',
          lecturerId: '',
          lecturerName: '',
          userContactId: this.id,
          userId: this.userId
        }
        baseParams = _.extend(baseParams, item)
        delete baseParams.id
        this.submitAppraise(baseParams, data)
      },
      save (data) {
        if (this.organizationApppraiseValue === '') {
          return tools.tip('请输入对培训机构的评分')
        }
        if (this.organizerApppraiseValue === '') {
          return tools.tip('请输入对主办单位的评分')
        }
        if (this.list.length === 0) {
          return tools.tip('没有课程表信息')
        }
        console.log(data)
        console.log('this.detail', this.detail)
        let baseParams = {
          classId: '',
          curriculumName: '',
          lecturerId: '',
          lecturerName: '',
          userContactId: this.id,
          userId: this.userId
        }
        baseParams = _.extend(baseParams, this.detail)
        delete baseParams.id
        // 主办单位
        if (this.routeName === 'organizer') {
          baseParams.zbdwId = this.detail.deptid
          baseParams.zbdwName = this.detail.deptname
          delete baseParams.institutionsId
          delete baseParams.institutionsName
        } else {
          delete baseParams.zbdwId
          delete baseParams.zbdwName
        }
        this.submitAppraise(baseParams, data)
      },
      submitAppraise (baseParams, data) {
        let params = []
        let _data = _.cloneDeep(data)
        _data.forEach(v => {
          v.score = v.value
          v.evaluationCriterionXmbm = v.xmbm
          v.evaluationCriterionPfxm = v.pfxm
          let temp = _.extend({}, baseParams, v)
          delete temp.id
          params.push(temp)
        })
        console.log('appraiseTeacher data', data)
        console.log('appraiseTeacher', params)
        this.fetchService.trainingplanpjSaveOrUpdateOfAll(params).then(res => {
          if (res.code !== 0) {
            return this.tools.alert(res.message || '获取数据失败')
          }
          return this.tools.alert(res.message || '获取数据失败')
        })
      },
      classList () {
        let params = {
          classId: this.detail.classId,
          limit: 10000,
          page: 1
        }
        this.fetchService.classList(params).then(res => {
          if (res.code !== 0) {
            return this.tools.tip(res.message || '获取数据失败')
          }
          this.list = res.data
        })
      },
      organization (key) {
        let standardKey = {
          organization: '1',
          organizer: '2',
          teacher: '3',
        }
        let params = {
          "type": standardKey[key] || "1"
        }
        if (this.standard[key].length > 0) {
          return false
        }
        return this.fetchService.trainingplanEvaluationCriterion(params).then(res => {
          console.log(res)
          if (res.code !== 0) {
            return this.tools.tip(res.message || '获取数据失败')
          }
          this.standard[key] = res.data
          return res
        })
      },
      saveData () {
        if (this.organizationApppraiseValue === '') {
          return tools.tip('请输入对培训机构的评分')
        }
        if (this.organizerApppraiseValue === '') {
          return tools.tip('请输入对主办单位的评分')
        }
        if (this.list.length === 0) {
          return tools.tip('没有课程表信息')
        }
        let flag = false
        this.list.map((v) => {
          if (!v.value || v.value === '') {
            flag = true
            return tools.tip(v.name + '课程评分不能为空')
          }
        })
        if (flag) {
          return
        }
        let result = []
        let pxjg = {
          userId: this.userId,
          userName: this.$store.state.userInfo.nickname,
          classId: this.detail.classId,
          className: this.detail.className,
          institutionsId: this.detail.institutionsId,
          institutionsName: this.detail.institutionsName,
          score: parseInt(this.organizationApppraiseValue),
          type: '1',
          userContactId: this.detail.id
        }
        result.push(pxjg)
        let zbdw = {
          userId: this.userId,
          userName: this.$store.state.userInfo.nickname,
          classId: this.detail.classId,
          className: this.detail.className,
          zbdwId: this.detail.deptid,
          zbdwName: this.detail.deptname,
          score: parseInt(this.organizerApppraiseValue),
          type: '2',
          userContactId: this.detail.id
        }
        result.push(zbdw)
        this.list.map((v) => {
          let kcb = {
            userId: this.userId,
            userName: this.$store.state.userInfo.nickname,
            classId: this.detail.classId,
            className: this.detail.className,
            lecturerId: v.lecturerId,
            lecturerName: v.lecturerName,
            curriculumName: v.name,
            score: parseInt(v.value),
            type: '3',
            userContactId: this.detail.id
          }
          result.push(kcb)
        })
        this.fetchService.trainingplanpjSaveOrUpdateOfAll(result).then(res => {
          if (res.code !== 0) {
            return this.tools.alert(res.message || '获取数据失败')
          }
          this.tools.alert(res.message || '获取数据失败')
          this.tools.back()
        })
      }
    }
  }
</script>

<style scoped>

</style>
